﻿// /*------------------------------------------------------------
// 工程名:    Ensemble
// 文件名:    DefaultPluginLoader.cs
// 编写人:    yhh
// 编写日期: 下午 5:02
// -------------------------------------------------------------*/

using System;
using System.Reflection;
using NLog;

namespace Ensemble
{
    public class DefaultPluginLoader : IPluginLoader
    {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();

        public event EventHandler<AssemblyResolveEventArgs> AssemblyResolve;
        public void Init()
        {
            var osName = Environment.OSVersion != null ? Environment.OSVersion.ToString() : "Unknown";
            string osFriendlyName = null;
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                if (Environment.OSVersion.Version >= new Version(10, 0, 0))
                    osFriendlyName = "Windows 10";
                else if (Environment.OSVersion.Version >= new Version(6, 3, 0))
                    osFriendlyName = "Windows 8.1";
                else if (Environment.OSVersion.Version >= new Version(6, 2, 0))
                    osFriendlyName = "Windows 8";
                else if (Environment.OSVersion.Version >= new Version(6, 1, 0))
                    osFriendlyName = "Windows 7";
                else if (Environment.OSVersion.Version >= new Version(6, 0, 0))
                    osFriendlyName = "Windows Vista";
                else if (Environment.OSVersion.Version >= new Version(5, 2, 0))
                    osFriendlyName = "Windows XP 64 Bit Edition";
                else if (Environment.OSVersion.Version >= new Version(5, 1, 0))
                    osFriendlyName = "Windows XP";
                else if (Environment.OSVersion.Version >= new Version(5, 0, 0))
                    osFriendlyName = "Windows 2000";
            }
            logger.Info(
                "环境信息: " + Environment.NewLine +
                "   当前目录: {0}" + Environment.NewLine +
                "   命令行: {1}" + Environment.NewLine +
                "   操作系统: {2}" + Environment.NewLine +
                "   64位操作系统: {3}" + Environment.NewLine +
                "   64位进程: {4}" + Environment.NewLine +
                "   CLR版本: {5}" + Environment.NewLine +
                "   进程数量: {6}",
                Environment.CurrentDirectory,
                Environment.CommandLine,
                osName + (osFriendlyName != null ? "(" + osFriendlyName + ")" : ""),
                Environment.Is64BitOperatingSystem,
                Environment.Is64BitProcess,
                Environment.Version,
                Environment.ProcessorCount);


            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        }

        private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            if (AssemblyResolve != null)
            {
                var resolveArgs = new AssemblyResolveEventArgs(args.Name);
                AssemblyResolve(this, resolveArgs);

                if (resolveArgs.IsResolved) return resolveArgs.ResolvedAssembly;
            }

            if (args.RequestingAssembly != null)
            {
                logger.Warn("无法解析程序集 '{0}'",args.RequestingAssembly);
            }
            else
            {
                logger.Warn("无法解析程序集 '{0}'",args.Name);
            }
            return null;
        }
    }
}